package com.example.product.enums;

import java.util.HashMap;
import java.util.Map;

public enum ProductTypeEnum {
    ERROR(-1, "Error"),
    Empty(0, ""),
    ELECTRONICS(1, "Electronics"),
    HOME_APPLIANCES(2, "Home Appliances"),
    CLOTHING(3, "Clothing");

    private Integer type_code;
    private String type_msg;

    private static final Map<Integer, ProductTypeEnum> TYPE_CODE_MAP = new HashMap<>();
    private static final Map<String, ProductTypeEnum> TYPE_MSG_MAP = new HashMap<>();

    static {
        for (ProductTypeEnum type : ProductTypeEnum.values()) {
            TYPE_CODE_MAP.put(type.type_code, type);
            TYPE_MSG_MAP.put(type.type_msg, type);
        }
    }

    ProductTypeEnum(Integer type_code, String type_msg) {
        this.type_code = type_code;
        this.type_msg = type_msg;
    }


    public Integer getTypeCode() {
        return type_code;
    }

    public String getTypeMsg() {
        return type_msg;
    }

    public static ProductTypeEnum fromTypeCode(Integer typeCode) {
        var resp = TYPE_CODE_MAP.get(typeCode);
        if(resp == null){
            return Empty;
        }
        return resp;
    }

    public static ProductTypeEnum fromTypeMsg(String typeMsg) {
        var resp = TYPE_MSG_MAP.get(typeMsg);
        if(resp == null){
            return Empty;
        }
        return resp;
    }

    public static Integer getTypeCodeByTypeMsg(String typeMsg) {
        return fromTypeMsg(typeMsg).getTypeCode();
    }

    public static String getTypeMsgByTypeCode(Integer typeCode) {
        return fromTypeCode(typeCode).getTypeMsg();
    }
}

